package pl.szpadel.android.gadu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LogonActivity extends Activity {

	static private final String TAG = "LogonActivity";
	
	// key holding message to be shown 
	static public final String EXTRA_LOGON_MESSAGE = "ELM";
	
	EditText mUinEditor;
	EditText mPasswordEditor;
	Button mConnectButton;
	TextView mMessage;
	
	/// Called when activity is created
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    // register with App
	    App.getInstance().setLogonActivity(this);
	    // start the service
        startService(new Intent(this, CommunicationService.class));
	    
	    
	    setContentView(R.layout.logon_window);
	    
		mMessage = (TextView)findViewById(R.id.LogonMessage);
	    mUinEditor = (EditText)findViewById(R.id.LogonUin);
	    mPasswordEditor = (EditText)findViewById(R.id.LogonPassword);
	    mConnectButton = (Button)findViewById(R.id.LogonButton);
	    
		// set button callback
		mConnectButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) { onConnectClicked(); }
			});
		
	}
	
	/// Called when destroyed
	public void onDestroy() {
		super.onDestroy();
		App.getInstance().setLogonActivity(null);
	}
	
	/// Called when started
	public void onStart() {
		super.onStart();
		
	}
	
	public void onResume() {
		super.onResume();
		AGLog.i(TAG, "resuming");
		Intent intent = getIntent();
		int res = intent.getIntExtra(EXTRA_LOGON_MESSAGE, 0);
		if (res != 0) {
			mMessage.setText(res);
			AGLog.d(TAG, "resuming with message: " + getText(res));
		} else {
			AGLog.d(TAG, "resuming with default message");
			mMessage.setText(R.string.msg_please_logon);
		}
		
		// init fields
		Config config = App.getInstance().getConfig();
		
		AGLog.i(TAG, "loading config, uin=" + config.getUin() + ", pwd=" + config.getPassword());
		
		if (config.getUin() > 0) {
			mUinEditor.setText(Long.toString(config.getUin()));
		} else {
			mUinEditor.clearComposingText();
		}
		
		if (config.getPassword().length() > 0) {
			mPasswordEditor.setText(config.getPassword());
		} else {
			mPasswordEditor.clearComposingText();
		}		
	}

	private void onConnectClicked() {
		AGLog.i(TAG, "connect button clicked");
		int uin = 0;
		try {
			uin = Integer.parseInt(mUinEditor.getText().toString());
		} catch(NumberFormatException e) {
			AGLog.e(TAG, "Unable to parse uin:" + mUinEditor.getText().toString());
			return;
		}
		String pwd = mPasswordEditor.getText().toString();
		
		if (uin > 0 && pwd.length() > 0 ) {
			Config config = App.getInstance().getConfig();
			
			config.setUin(uin);
			config.setPassword(pwd);
			config.setConnected(true);
			
			// make sue status is not 'not available', otherwise server will disconnect us just after connection
			Status status = config.getStatus();
			if (status.getGGStatusNoDesc() == Status.GG_STATUS_NOT_AVAIL) {
				// change to invisible
				String desc = status.getDescription();
				config.setStatus(new Status(Status.GG_STATUS_INVISIBLE, desc));
			}
			
			config.write(this);
			
			// force reconnect
			App.getInstance().getCommunicationService().reconnectNow();
			AGLog.i(TAG, "finishing");
			Toast.makeText(this, R.string.toast_connecting, Toast.LENGTH_SHORT).show();
			
			// go to contact list
			startActivity(new Intent(this, ContactListActivity.class));

		} else {
			AGLog.e(TAG, "invalid credentials, pwd="+pwd+", uin="+uin);
		}
	}
	
    @Override
    protected void onNewIntent (Intent intent) {
    	setIntent(intent);
    }
 	
	
}
